package com.mx.zb.mng.ctrl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.mx.zb.mng.bo.Agent;
import com.mx.zb.mng.bo.Operator;
import com.mx.zb.mng.bo.Role;
import com.mx.zb.mng.cache.OperatorCaches;
import com.mx.zb.mng.dao.AgentMapper;
import com.mx.zb.mng.dao.ModuleMapper;
import com.mx.zb.mng.dao.OperatorMapper;
import com.mx.zb.mng.dao.RoleMapper;
import com.mx.zb.mng.dao.RoleModuleRelaMapper;
import com.mx.zb.mng.utils.AjaxResult;
import com.mx.zb.mng.utils.Constant;
import com.mx.zb.mng.utils.UnionCode;
import com.mx.zb.mng.utils.context.ContextThread;
import com.mx.zb.mng.utils.db2.Page;
import com.mx.zb.mng.utils.db2.PageHelper;
import com.mx.zb.mng.utils.ex.RestAnnotation;
/**
 * 角色模块
 * @author 张衡
 *
 */
@RestController
@RestAnnotation
@RequestMapping("/a/sys")
public class SystemRestCtrl {
	
	
	@Autowired
	RoleMapper roleMapper;
	
	@Autowired
	OperatorMapper operatorMapper;
	
	@Autowired
	RoleModuleRelaMapper roleModuleRelaMapper;
	
	@Autowired
	OperatorCaches operatorCaches;
	
	@Autowired
	ModuleMapper moduleMapper;
	
	@Autowired
	AgentMapper agentMapper;
	
	Logger log = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 添加角色
	 * @param o
	 * @return
	 */
	@RequestMapping(value= {"/role"}, method= {RequestMethod.PUT})
	@Transactional
	public AjaxResult addRole(@RequestBody Role o) {
		String agentId=ContextThread.get().getAgentId();
		String name=o.getName();
	    Role role=roleMapper.selectByName(agentId,name);
	    if (null!=role) {
	    	return new AjaxResult().failure("角色名不能重复");
		}
		o.setId(UnionCode.getShortUuid());
		o.setCreateDate(new Date());
		o.setUpdateDate(o.getCreateDate());
		o.setCreateOper(ContextThread.get().getOperatorId());
		o.setUpdateOper(ContextThread.get().getOperatorId());
		o.setAgentId(ContextThread.get().getAgentId());
		roleMapper.insert(o);
		roleModuleRelaMapper.insertList(o.getRelaIds(), o);		
		return new AjaxResult().success();
	}
	/**
	 * 修改角色
	 * @param o
	 * @return
	 */
	@RequestMapping(value= {"/role"}, method= {RequestMethod.POST})
	@Transactional
	public AjaxResult modifyRole(@RequestBody Role o) {
		String agentId=ContextThread.get().getAgentId();
		String name=o.getName();
		String id=o.getId();
	    Role role=roleMapper.selectByName1(id,agentId,name);
	    if (null!=role) {
	    	AjaxResult result=new AjaxResult();
	    	result.setStatus("2");
	    	result.setMessage("角色名不能重复");
	    	return result;
	    }
		roleMapper.updateByPrimaryKeySelective(o);
		roleModuleRelaMapper.deleteByRoleId(o.getId());
		o.setCreateDate(new Date());
		o.setUpdateDate(o.getCreateDate());
		o.setCreateOper(ContextThread.get().getOperatorId());
		o.setUpdateOper(ContextThread.get().getOperatorId());
		o.setAgentId(ContextThread.get().getAgentId());
		
		roleModuleRelaMapper.insertList(o.getRelaIds(), o);
		
		operatorCaches.refreshSideBarList(o.getId());
		
		return new AjaxResult().success();
	}
	/**
	 * 删除角色
	 * @param o
	 * @return
	 */
	@RequestMapping(value= {"/role"}, method= {RequestMethod.DELETE})
	@Transactional
	public AjaxResult deleteRole(@RequestBody Role o) {	
		operatorMapper.deleteByRole(o.getId());
		roleMapper.deleteByPrimaryKey(o.getId());
		roleModuleRelaMapper.deleteByRoleId(o.getId());
		return new AjaxResult().success();
	}
	/**
	 * 角色列表
	 * @return
	 */
	@RequestMapping(value= {"/roles"}, method= {RequestMethod.GET})
	public AjaxResult getRoleList() {
		Agent agent=agentMapper.selectByPrimaryKey(ContextThread.get().getAgentId());
		String loginName=agent.getPhone();
		Operator operator =operatorMapper.selectByName(loginName,null);
		Role o = new Role();
		o.setAgentId(agent.getId());
		if (!Constant.headquarters.equals(agent.getType())) {
		o.setId(operator.getRoleId());
		}
		PageHelper.startPage();
		Page<Role> rolePages = roleMapper.queryPage(o);
		return new AjaxResult().success(rolePages);
		
	}
	/**
	 * 获取角色
	 * @param r
	 * @return
	 */
	@RequestMapping(value= {"/torole"}, method= {RequestMethod.POST})
	public AjaxResult getRole(@RequestBody Role r) {
		r=roleMapper.selectByPrimaryKey(r.getId());
		return new AjaxResult().success(r);
	}
	/**
	 * 获取角色
	 * @param o
	 * @return
	 */
	@RequestMapping(value= {"/role"}, method= {RequestMethod.GET})
	public AjaxResult getRoleForUpdate(Role o) {
		
		Role r = roleMapper.selectByPrimaryKey(o.getId());		
		
		List<String> relaIds=roleModuleRelaMapper.selectIds(o.getId());
		
		r.setRelaIds(relaIds);
		
		return new AjaxResult().success(r);
	}
	/**
	 * 获取员工和代理商信息
	 * @param o
	 * @return
	 */
	@RequestMapping(value= {"/operator"}, method= {RequestMethod.POST})
	public AjaxResult getOperator(@RequestBody Operator o) {
		Operator r = operatorMapper.selectByPrimaryKey(o.getId());	
		Agent a = agentMapper.selectByPrimaryKey(r.getAgentId());		
		return new AjaxResult().success(a);
	}
	

/*	@ResponseBody
	@RequestMapping(value= {"/operatorajax"},method= {RequestMethod.GET})
	public void toOperatorAjax(HttpServletRequest request,HttpServletResponse response,String id)  {	
		try {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		if (null!=id) {
			List<String> list=roleModuleRelaMapper.selectIds(id);
			List<Module> modules=new ArrayList<Module>();
			for(String mid:list) {
				modules.add(moduleMapper.selectByPrimaryKey(mid));
			}
			PrintWriter writer = null;
			try {
				writer = response.getWriter();
				String result=JSON.toJSONString(modules);
				writer.print(result);
			} catch (IOException e) {
				e.printStackTrace();
			}
			finally {
				writer.close();
			}
			
		}
		
	}*/
	
	
}
